<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
	</head>
	<body>


		<script type="text/javascript">
			// 字符串  
			let obj = {
				a:10,
				b:20,
				c:{
					x:8,
					y:6
				},
				d:NaN,
				e:undefined,
				f:null,
				g:0,
				h:'',
				i:function(){}
			}
			let obj1 = deepCopy(obj)
			obj1.a =888
			obj1.c.x = 666
			console.log(obj1,obj);
			
			function deepCopy(data){
				let st = typeof data
				if(st==='object'&&data!==null){
					if(Array.isArray(data)){
						console.log(777);
						let newArr = []
						for (let i = 0; i < data.length; i++) {
							newArr.push(deepCopy(data[i]) )
						}
						return newArr
					}else{
						console.log(66);
						let obj = {}
						for (let key in data) {
							obj[key] =    deepCopy(data[key])
						}
						return obj
					}
				}
				return data
			}
			
			
			
		</script>
		<!-- 
		一 JSON 
		JSON 是一种轻量级的数据交换格式
		
			
		 二.深拷贝
		  1.1 一维数组深拷贝
		   ①for循环  
		 	let arr1 = []
		 	for (let i = 0; i < arr.length; i++) {
		 		arr1.push(arr[i])
		 	}	
		 	
		 	②let arr1 = [].concact(arr)
		 	缺点只能拷贝一层
		  1.2 一维对象 深拷贝
		  ①对象的循环 	  
		  let obj1 = {}
		  for (let key in obj) {
		  	obj1[key] = obj[key]
		  }
		 ②或者是 let obj1 = Object.assign({},obj)
		 缺点只能拷贝一层
		 
		  1.3 JSON
		  能够 深层次拷贝
		  let obj1 = JSON.parse(JSON.stringify(obj))
		  缺点  会丢失 NaN跟undefined以及 函数, Infinity 的值
		 
		 1.4 使用递归深拷贝
		 	function deepCopy(data) {
		 		let st = typeof data
		 		if(st=='number'||st=='string'||st=='boolean'||st==='undefined'||data===null||st=='function'){
		 			return data
		 		}
		 		if(Array.isArray(data)){
		 			let arr = []
		 			for (let i = 0; i < data.length; i++) {
		 				arr.push(deepCopy(data[i]))
		 			}	
		 			return arr
		 		}else{
		 			let obj = {}
		 			for (let key in data) {
		 				obj[key] = deepCopy(data[key])
		 			}
		 			return obj
		 		}
		 	}-->
	</body>
</html>
